Skip to content

A bulk write to an audited table is in the trail, row by row - #17

Merged
SerhiyGreench merged 1 commit into
masterfrom
bulk-writes-reach-the-trail
Nov 18, 2026
Merged

SerhiyGreench merged 1 commit into
masterfrom
bulk-writes-reach-the-trail

Conversation

@SerhiyGreench

Copy link
Copy Markdown
Member

What

  • A bulk delete or update left no row in the audit trail. removeBulk and updateBulk sent a bare count (deleteAndCount, updateAndCount). A count goes through the runtime's execute, which returns no rows, so onRow never fires; the trail records a write from the rows a statement returns, so it recorded nothing. That held for soft deletes too, since stamp keeps a statement's RETURNING and there was none to keep. On a recorded table both now send … RETURNING and count the rows as they stream. The trail is written right after the statement, as it already is for an update by id.
  • A select on a write could hide the row from the trail. create, createBulk, update and upsert narrowed RETURNING to the selected columns, so the trail kept only those, and nothing at all when the id was not among them. On a recorded table the statement now returns the whole row, and the caller is handed only what it selected.
  • Every other table is as cheap as before. A table in auditExclude keeps the bare count and the narrowed RETURNING. repositoriesFor learns which tables are recorded from the relation map deriveDataLayer returned, because that map is all a caller passes today; the new audited option says it outright, and audited: {} keeps the bare count everywhere, for a service with no trail.
Call, on a recorded table Before After
removeBulk (hard or soft) count only, no trail row … RETURNING, one trail row per row
updateBulk count only, no trail row … RETURNING, one trail row per row
createBulk / create / update / upsert with a select RETURNING the select; trail partial or empty RETURNING the row; caller gets the select
any of these, on a table the trail leaves out unchanged unchanged

Upserts were checked as well: they always returned their row, so the trail had one; it records an upsert that updated as an INSERT, which is unchanged here.

Tests

test/unit/repository.spec.ts now builds its layer with the real audit middleware, its pool swapped for one that keeps what it inserts:

  • a hard bulk delete is one DELETE … RETURNING, with a DELETE trail row per row and the count
  • a soft bulk delete is one UPDATE … SET "deletedAt" … RETURNING, recorded as DELETE per row
  • a bulk update is one UPDATE … RETURNING, with an UPDATE trail row per row
  • a table the trail leaves out keeps the bare count for both, and so does audited: {}
  • a bulk create, an upsert and an update with a select return the whole row to the trail and only the select to the caller

Six of these fail on master. npm test 115/115, npm run lint and npm run format:check clean.

Checked against a consuming service as well: its integration suite (175 tests) passes on this build, and removing a record through removeBulk now leaves the DELETE row in its trail that 3.2.0 did not.

- `removeBulk` and `updateBulk` sent a bare count (`deleteAndCount`,
  `updateAndCount`). A count returns no rows, and the trail records a
  write from the rows a statement returns, so a bulk delete or update,
  soft or hard, left no row in the trail at all. On a recorded table they
  now send `… RETURNING` and count the rows as they stream; the trail is
  written right after the statement, as it is for an update by id.
- A `select` on `create`, `createBulk`, `update` or `upsert` narrowed the
  RETURNING, so the trail saw only the selected columns, and nothing at
  all when the id was not among them. On a recorded table the statement
  now returns the whole row and the caller is handed only the select.
- Every other table keeps the bare count and the narrowed RETURNING.
  `repositoriesFor` learns which tables are recorded from the relation
  map `deriveDataLayer` returned, since that map is all a caller passes;
  `audited` says it outright, and `audited: {}` keeps the count
  everywhere.
@SerhiyGreench
SerhiyGreench merged commit 1684dde into master Nov 18, 2026
10 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 18, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant